package com.wanmait.gohome.controller.web;

import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.pojo.Comment;
import com.wanmait.gohome.pojo.Mark;
import com.wanmait.gohome.pojo.Task;
import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.MarkService;
import com.wanmait.gohome.service.TaskService;
import com.wanmait.gohome.util.Pager;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/web/task")
public class taskController {
    @Resource
    private TaskService taskService;
    @Resource
    private MarkService markService;
    /**
     * 跳转到发布任务ajax界面
     */
    @GetMapping("releaseTaskAjax")
    @ResponseBody
    public ModelAndView releaseTaskAjax()
    {
        ModelAndView mv=new ModelAndView();
        PageInfo<Task> tasks=taskService.selectTask();
        mv.addObject("tasks",tasks);
        return mv;
    }


    /**
     * 跳转到显示任务任务页面    今日最热任务
     */
    @GetMapping("releaseTask")
    public ModelAndView releaseTask()
    {
        ModelAndView mv = new ModelAndView();
        List<Task> tasks = taskService.findTodayHot(5);
        mv.addObject("tasks",tasks);
        List<Mark>marks=markService.selectTitle();
        mv.addObject("marks",marks);
        return mv;
    }

    /**
     * 跳转到发布任务界面
     */
    @GetMapping("publishTask")
    public void publishTask()
    {

    }
    /**
     * post提交发布任务
     */
    @PostMapping("publishTask")
    public String publishTask(Task task,MultipartFile pic1,HttpServletRequest request)
    {
        String fileName="";
        String ext="";
        try {
            ext = FilenameUtils.getExtension(pic1.getOriginalFilename());//获取扩展名;
            fileName = UUID.randomUUID()+"."+ext;//这个是随机数加上扩展名;
            String realPath = request.getServletContext().getRealPath("/web/userinfo/img/taskImages"+"/"+fileName);//获得图片路径加上图片名字;
            File file=new File(realPath);//保存图片的路径;
            pic1.transferTo(file);//上传图片到指定路径;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(ext!="")
        {
            task.setPic(fileName);
        }
        taskService.insertTask(task);
        return "redirect:/web/task/releaseTask";
    }


    /**
     *
     * @param task
     * @param pic1   个人中心   修改任务
     * @param request
     * @return
     */
    @PostMapping("updateByTaskId")
    public String updateByTaskId(MultipartFile pic1,HttpServletRequest request,Task task)
    {
        String fileName="";
        String ext="";
        try {
            ext = FilenameUtils.getExtension(pic1.getOriginalFilename());//获取扩展名;
            fileName = UUID.randomUUID()+"."+ext;//这个是随机数加上扩展名;
            String realPath = request.getServletContext().getRealPath("/web/userinfo/img/taskImages/"+fileName);//获得图片路径加上图片名字;
            File file=new File(realPath);//保存图片的路径;
            pic1.transferTo(file);//上传图片到指定路径;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(ext!="")
        {
            task.setPic(fileName);
        }
        taskService.updateByTaskId(task);
        return "redirect:/web/task/releaseTask";
    }
    /**
     * get方式点击发布任务跳到发布任务详情页
     */
    @GetMapping("details")
    public ModelAndView details(Integer id)
    {
        ModelAndView mv=new ModelAndView();
        Task task=taskService.findById(id);
        taskService.updateBrowse(id);
        mv.addObject("task",task);

        List<Task> tasks = taskService.findTodayHot(5);
        mv.addObject("tasks",tasks);
        return mv;
    }

    /**
     * 任务发布评论
     * @param comment
     * @return
     */
    @PostMapping("details")
    @ResponseBody
    public void taskComments(Comment comment)
    {
        taskService.updateTaskComments(comment.getTaskId());
        taskService.insertTaskComments(comment);

    }




    /**
     *
     * @param idStr  显示任务评论
     */
    @RequestMapping("taskComments")
    @ResponseBody
    public ModelAndView taskCommnets(String idStr)
    {
        Integer id = Integer.parseInt(idStr);
        PageInfo<Comment> pageInfo=taskService.findTaskComments(id);
        pageInfo.getList().forEach(comment -> System.out.println(comment.getComments()));
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    /**
     * solr搜索 ajax页面
     */
    @GetMapping("releaseTaskSolr")
    @ResponseBody
    public ModelAndView taskCommentsSolr(String title)
    {
        Pager pager = new Pager();
        List<Task> tasks = taskService.findByTitles(title,pager);
        ModelAndView mv = new ModelAndView();
        mv.addObject("tasks", tasks);
        mv.addObject("pager",pager);
        return mv;
    }

    /**
     * 添加任务的评论(二级评论)
     * @param multistageComments
     */
    @GetMapping("taskMultistageComments")
    @ResponseBody
    public void  insertTaskMultistageComments(Comment multistageComments)
    {
        taskService.insertTaskMultistageComments(multistageComments);
    }

    /**
     * 查找任务的评论(二级评论)
     * @param commentId
     * @return
     */
    @PostMapping("taskMultistageComments")
    @ResponseBody
    public List<Comment> findTaskMultistageComments(String commentId)
    {
        Integer id = Integer.parseInt(commentId);
        List<Comment> comments1 = taskService.findTaskMultistageComments(id);
        System.out.println(comments1);
        return comments1;
    }

    /**
     * 这个就是任务列表 根据mark状态 显示任务的方法
     * @param markId
     * @return
     */
    @RequestMapping("stayAcceptAjax")
    @ResponseBody
    public ModelAndView releaseTaskAjax(Integer markId)
    {
        ModelAndView mv=new ModelAndView();
        if(markId==null||markId==0)
        {
            PageInfo<Task> tasks=taskService.selectTask();
            mv.addObject("tasks",tasks);
        }
        else
        {
            PageInfo<Task>tasks=taskService.selectStayAccept(markId);
            mv.addObject("tasks",tasks);
        }


        return mv;
    }


    /**
     *
     * @param taskId
     * @param request  接受任务
     * @return
     */
    @GetMapping("acceptTask")
    @ResponseBody
    public String acceptTask(Integer taskId,HttpServletRequest request)
    {
        Userinfo userinfo = (Userinfo)request.getSession().getAttribute("userinfo");
        if(userinfo==null){
            request.getSession().setAttribute("goUrl","/GoHome/web/task/details?id="+taskId);
            return "login";
        }else{
            taskService.acceptTask(userinfo.getId(),taskId);
            return "ok";
        }
    }

}
